/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simframe.simulation;

/**
 *
 * @author Steve Noble
 */
public class Range {

    double max = Double.MIN_VALUE;
    double min = Double.MAX_VALUE;

    public double getMax() {
        return max;
    }

    public double getMin() {
        return min;
    }

    public void setMinMax(double min, double max) {
        this.max = max;
        this.min = min;
    }

    public void update(double val) {

        if (val > max) {
            max = val;

            double log = Math.log10(max);

            double adjustedLog = Math.floor(log);

            double lowMax = Math.pow(10, adjustedLog);

            double secundo = max - lowMax;

            if (secundo > 0) {
                log = Math.log10(secundo);

                adjustedLog = Math.ceil(log);

                max = lowMax + Math.pow(10, adjustedLog);
            } else {
                max = lowMax;
            }
        }

        if (val < min) {

            min = val;
        }


    }

    public int map(double val, int imin, int imax) {

        assert (max > min);
        // based on max and min

        return (int) Math.round(imin + (val - min) * (imax - imin) / (max - min));
    }

    // inverse map
    public double invMap(double val, double imin, double imax) {

        assert (imax != imin);
        // based on max and min

        return min + (val - imin) * (max - min) / (imax - imin);
    }


    /*
     *
     * Maps a number from 0 to 1 to range
     */
    public double mapToRange(double val) {

        assert (max > min);
        // based on max and min

        return min + val * (max - min);
    }

    public Range(double min, double max) {
        this.min = min;
        this.max = min;
    }

    // default
    public Range() {
    }
}
